﻿using Bussiness.Interface;
using Common;
using Common.Model;
using Model;
using System;
using System.Collections.Generic;

namespace Bussiness.Service
{
    public class BookService : BaseService, IBookService
    {
        /// <summary>
        /// 加入购入车等一切操作
        /// </summary>
        /// <param name="bookid">图书编号</param>
        /// <param name="userid">用户编号</param> 
        /// <param name="num">图书数量</param>
        /// <param name="bookingStatus">订单类型</param>
        /// <returns>是否成功 bool类型</returns>
        public Result<bool> BookCop(string bookid, string userid, int num, Enums.BookingStatus bookingStatus)
        {
            Result<bool> result = new Result<bool>();
            try
            {
                Books books = new Books();
                books = base.Select<Books>(p => p.BookID == bookid).Data[0];
                Orders orders = new Orders();
                orders.BookingStatus = bookingStatus.ToString();
                Result<List<Orders>> list = new Result<List<Orders>>();
                list = base.Select<Orders>(p => p.BookID == bookid, q => q.UserID == userid);
                if (list.Data != null && list.Data.Count > 0)
                {
                    orders = list.Data[0];
                    result = base.Update(orders);
                }
                else
                {
                    orders.OrderID = Guid.NewGuid().ToString();
                    orders.BookID = bookid;
                    orders.UserID = userid;
                    orders.Num = num;
                    orders.Price = num * books.Price;
                    orders.BookingTime = DateTime.Now;
                    result = base.Insert(orders);
                }
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }
    }
}
